//
//  NSString+RRZ.m
//  RenRenZhao
//
//  Created by 邓乐 on 2017/11/30.
//  Copyright © 2017年 rrz. All rights reserved.
//

#import "NSString+RRZ.h"
#import <OpenUDID.h>
#import "EMSecurityDBModel.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSString (RRZ)

+(NSString*)safeString:(NSString*)string
{
    if (string == nil) {
        return @"";
    }
    if (![string isKindOfClass:[NSString class]]) {
        return  @"";
    }
    return string;
    
}

+ (NSString *)phoneFormat:(NSString *)sourceS{
    if (sourceS.length) {
        
    }else{
        return nil;
    }
    NSMutableString *tmpS = [NSMutableString stringWithString:sourceS];
    for (int i = 3; i < tmpS.length; i += 5) {
        [tmpS insertString:@" " atIndex:i];
    }
    return tmpS;
}

+ (BOOL)isPureInt:(NSString*)string{
    if (string.length > 0) {
        NSScanner* scan = [NSScanner scannerWithString:string];
        int val;
        return[scan scanInt:&val] && [scan isAtEnd];
    }
    return YES;
}

//验证手机号长度 是否为纯数字
+ (BOOL)isPhone:(NSString *)string
{
    if (![string hasPrefix:@"1"]) {
        return NO;
    }
    if (string && string.length == 11) {
        if ([self isPureInt:string]) {
            return YES;
        }
    }
    return NO;
}

+ (BOOL)isPassword:(NSString *)string{

    NSString *pattern = @"^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,20}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",pattern];
    BOOL isMatch = [pred evaluateWithObject:string];
    return isMatch;
}



+ (NSInteger)compareVersion:(NSString *)v1 to:(NSString *)v2 {
    // 都为空，相等，返回0
    if (!v1 && !v2) {
        return 0;
    }

    // v1为空，v2不为空，返回-1
    if (!v1 && v2) {
        return -1;
    }

    // v2为空，v1不为空，返回1
    if (v1 && !v2) {
        return 1;
    }

    // 获取版本号字段
    NSArray *v1Array = [v1 componentsSeparatedByString:@"."];
    NSArray *v2Array = [v2 componentsSeparatedByString:@"."];
    // 取字段最大的，进行循环比较
    NSInteger bigCount = (v1Array.count > v2Array.count) ? v1Array.count : v2Array.count;

    for (int i = 0; i < bigCount; i++) {
        // 字段有值，取值；字段无值，置0。
        NSInteger value1 = (v1Array.count > i) ? [[v1Array objectAtIndex:i] integerValue] : 0;
        NSInteger value2 = (v2Array.count > i) ? [[v2Array objectAtIndex:i] integerValue] : 0;
        if (value1 > value2) {
            // v1版本字段大于v2版本字段，返回1
            return 1;
        } else if (value1 < value2) {
            // v2版本字段大于v1版本字段，返回-1
            return -1;
        }

        // 版本相等，继续循环。
    }

    // 版本号相等
    return 0;
}

- (NSString *)md5 {
    const char *cStr = [self UTF8String];
    unsigned char result[16];
    CC_MD5( cStr, (unsigned int)strlen(cStr), result ); // This is the md5 call
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}

+ (NSString *)deviceUUID
{
    static NSString* deviceUUID = nil;
    
    if (deviceUUID == nil) {
        @try {
            NSString* uuidKey = @"emdeviceuuid";
            deviceUUID = (NSString *)[EMSecurityDBModel objectFromKey:uuidKey];
            if (SAFE_STRING(deviceUUID).length <= 0) {//keychain中找不到，再生成一个新的uuid
                deviceUUID = [OpenUDID value];
                [EMSecurityDBModel storeWithKey:uuidKey value:deviceUUID];
            }
        } @catch (NSException *exception) {
            //DebugLog(@"Failed to get UUID From KeyChain!!!!!!!!!!!!!!!!\nException:%@",exception);
        } @finally {
            if (SAFE_STRING(deviceUUID).length <= 0 ) {
                NSString* uuidContent = [NSString stringWithFormat:@"%lf%d%d%d", [NSDate date].timeIntervalSince1970,arc4random()%937, arc4random()%887, arc4random()%983];
                deviceUUID = [uuidContent md5];
            }
        }
    }
    return deviceUUID;
}

@end
